package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.model.ExpertModel;
import com.app.model.IncubatorsArticleModel;
import com.app.model.IncubatorsModel;
import com.app.service.AdminUserService;
import com.app.service.ExpertService;
import com.app.service.IncubatorsArticleService;
import com.app.service.IncubatorsService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.ExpertVo;
import com.app.vo.IncubatorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 专家
 * @Author: N
 * @Date: 2022/10/29 15:26
 */
@RestController
@RequestMapping("/")
public class ExpertBackstageController {
    @Autowired
    ExpertService expertService;

    @Autowired
    AdminUserService adminUserService;

    /**
     *获取专家列表
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/getExpertPage")
    public Result<Object> getIncubatorsPage(HttpServletRequest request, ExpertModel model){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        ExpertVo vo = expertService.getListByModel(model);
        return Result.success(vo);
    }

    @RequestMapping(value = "editExpert", method= RequestMethod.POST)
    public Result<Object> editExpert(HttpServletRequest request, @RequestBody ExpertModel model){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if(model.getId()!=null&&model.getId()>0)
        {
            expertService.update(model);
        }else{
            expertService.insert(model);
        }
        return Result.success("操作成功");
    }

    @GetMapping("/delExpert")
    public Result<Object> delExpertPage(HttpServletRequest request, Integer id){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        expertService.delete(id);
        return Result.success("操作成功");
    }

}
